package com.fmsz.xmly.fragments.downloadfgts.hotrecommendfgts;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.download.RecommendAdapter;
import com.fmsz.xmly.callbacks.DownloadRecommendService;
import com.fmsz.xmly.entitys.download.RecommendEntity;
import com.fmsz.xmly.utils.Urls;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseHotRecommendFragment extends Fragment implements Callback<RecommendEntity> {
    private LinearLayout linearLayout;
    private RecyclerView recyclerView;
    private RecommendAdapter adapter;
    private List<RecommendEntity.SubEntity> list;

    private String tagg;

    public String getTagg() {
        return tagg;
    }

    public void setTagg(String tagg) {
        this.tagg = tagg;
    }

    public BaseHotRecommendFragment() {
        // Required empty public constructor
    }

    public static BaseHotRecommendFragment newInstance(String tagg) {

        Bundle args = new Bundle();
        args.putString("tagg", tagg);
        BaseHotRecommendFragment fragment = new BaseHotRecommendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_base_hot_recommend, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout_recommend);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycleView_recommend);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        list = new ArrayList<>();
        adapter = new RecommendAdapter(getContext(), list);
        recyclerView.setAdapter(adapter);
        Bundle bundle = getArguments();
        tagg = bundle.getString("tagg");
        switch (tagg) {
            case Urls.TYPE_HOT:
                Retrofit build1 = new Retrofit.Builder()
                        .baseUrl("http://mobile.ximalaya.com/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                build1.create(DownloadRecommendService.class).getRecommendDatas(Urls.TYPE_HOT).enqueue(this);
                break;
            case Urls.TYPE_RECENT:
                Retrofit build2 = new Retrofit.Builder()
                        .baseUrl("http://mobile.ximalaya.com/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                build2.create(DownloadRecommendService.class).getRecommendDatas(Urls.TYPE_RECENT).enqueue(this);
                break;
            case Urls.TYPE_CLASSIC:
                Retrofit build3 = new Retrofit.Builder()
                        .baseUrl("http://mobile.ximalaya.com/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                build3.create(DownloadRecommendService.class).getRecommendDatas(Urls.TYPE_CLASSIC).enqueue(this);
                break;
        }
    }

    @Override
    public void onResponse(Call<RecommendEntity> call, Response<RecommendEntity> response) {
        RecommendEntity body = response.body();
        list.addAll(body.getList());
        if (list != null) {
            linearLayout.setVisibility(View.INVISIBLE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<RecommendEntity> call, Throwable t) {

    }
}
